package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.DrmoPackage;
import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.Manufacturer;
import mil.af.amc.adpetracker.service.DrmoPackageService;
import mil.af.amc.adpetracker.service.ManufacturerService;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.util.Collections;
import java.util.List;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */
@Service("manufacturerViewHelper")
public class ManufacturerViewHelper implements PageContextAwareBean  {

    private PageContext pageContext;

    @Autowired
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    private DrmoPackageService drmoPackageService;

    @Autowired
    public void setDrmoPackageService(DrmoPackageService drmoPackageService) {
        this.drmoPackageService = drmoPackageService;
    }

    private ManufacturerService manufacturerService;

    @Autowired
    public void setManufacturerService(ManufacturerService manufacturerService) {
        this.manufacturerService = manufacturerService;
    }

    private int typeId;

    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    public List<Manufacturer> getAll() {

        List<Manufacturer> resultList = manufacturerService.getAll();
        Collections.sort(resultList);
        return resultList;

    }

    public List<Manufacturer> getAllByType() {

        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        int packageId = Integer.parseInt(request.getParameter("packageId"));

        DrmoPackage pkg = drmoPackageService.getById(packageId);

        int typeId = ((EquipmentItem[]) pkg.getIdentifiedItems().toArray())[0].getEquipmentType().getId();

        if (typeId<1) {
            return null;
        }
        List<Manufacturer> resultList = manufacturerService.getManufacturerByEquipmentType(typeId);
        Collections.sort(resultList);
        return resultList;
    }
}
